package com.itheima.herbGarden.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.herbGarden.common.R;
import com.itheima.herbGarden.dto.SetmealDto;
import com.itheima.herbGarden.entity.Category;
import com.itheima.herbGarden.entity.Setmeal;
import com.itheima.herbGarden.service.CategoryService;
import com.itheima.herbGarden.service.SetmealDishService;
import com.itheima.herbGarden.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @classname SetmealController
 * @Author: MiaoMiao
 * @Description:
 * @DateTime: 2022/11/21 0021  下午 19:34
 **/

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){      //提交json格式的数据到服务端
        log.info("套餐信息:{}",setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //根据name进行模糊查询
        queryWrapper.like(name != null,Setmeal::getName,name);
        //更新时间降序排列
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,queryWrapper);     //page()进行查询

        BeanUtils.copyProperties(pageInfo,dtoPage,"Records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) ->{
           SetmealDto setmealDto = new SetmealDto();

           BeanUtils.copyProperties(item,setmealDto);

           Long categoryId = item.getCategoryId();
           Category category = categoryService.getById(categoryId);
           if (category != null) {
               //获取分类名称
               String categoryName = category.getName();
               setmealDto.setCategoryName(categoryName);

           }
           return setmealDto;
       }).collect(Collectors.toList());

        dtoPage.setRecords(list);

        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {             //可以批量删除
        log.info("ids:{}",ids);

        setmealService.removeWithDish(ids);

        return R.success("套餐删除成功");
    }

    /**
     * 根据条件查询套餐数据
     * */

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);


    }

}
